// pages/service/detail/detail.js
const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    refund: {},
    inputForm: {
      reason: '',
      phone: ''
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      refund: JSON.parse(options.refund)
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * input value
   */
  reasonValue: function(e) {
    this.setData({
      'inputForm.reason': e.detail.value
    })
  },

  phoneValue: function(e) {
    this.setData({
      'inputForm.phone': e.detail.value
    })
  },

  /**
   * 
   */
  refundsBtn: function(e) {
    let that =this,
        inputForm = that.data.inputForm,
        refund = that.data.refund

    if(inputForm.reason == '') {
      wx.showToast({
        icon: 'none',
        title: '请填写退款说明',
      })
      return false
    }

    if (inputForm.phone == '') {
      wx.showToast({
        icon: 'none',
        title: '请填写联系电话',
      })
      return false
    }

    wx.showModal({
      title: '提示',
      content: '是否确认退款',
      success: res => {
        if(res.confirm) {
          wx.showLoading({
            icon: 'none',
            title: app.globalData.loadText,
          })
          app.getAccessToken(app.getPageUrl(getCurrentPages())).then(function (accessToken) {
            wx.request({
              url: app.globalData.hostAPI + '/api/v1/refund-order/create?access_token=' + accessToken,
              data: {
                order_id: refund.order_id,
                order_item_id: refund.item.order_item_id,
                refund_reason: inputForm.reason,
                phone: inputForm.phone
              },
              success: res => {
                if (res.data.status === 1) {
                  wx.hideLoading()
                  wx.showModal({
                    showCancel: false,
                    title: '提示',
                    content: '申请提交成功',
                    success: res => {
                      if(res.confirm) {
                        wx.navigateTo({
                          url: "../../service/service?active='all'",
                        })
                      }
                    }
                  })
                } else {
                  wx.hideLoading()
                  wx.showToast({
                    icon: 'none',
                    title: res.data.msg,
                  })
                }
              },
              fail: res => {
                wx.hideLoading()
                wx.showToast({
                  icon: 'none',
                  title: res.data.msg,
                })
              }
            })
          })
        }
      }
    })
  }
})